package com.yunxi.module.system.controller.admin.dict;

import com.yunxi.framework.apilog.core.annotation.ApiAccessLog;
import com.yunxi.framework.apilog.core.enums.OperateTypeEnum;
import com.yunxi.framework.common.enums.CommonStatusEnum;
import com.yunxi.framework.common.pojo.CommonResult;
import com.yunxi.framework.common.pojo.PageParam;
import com.yunxi.framework.common.pojo.PageResult;
import com.yunxi.framework.common.util.object.BeanUtils;
import com.yunxi.framework.excel.util.ExcelUtils;
import com.yunxi.module.system.model.dict.DictDataDO;
import com.yunxi.module.system.service.dict.DictDataService;
import com.yunxi.module.system.vo.admin.dict.data.DictDataPageReqVO;
import com.yunxi.module.system.vo.admin.dict.data.DictDataRespVO;
import com.yunxi.module.system.vo.admin.dict.data.DictDataSaveReqVO;
import com.yunxi.module.system.vo.admin.dict.data.DictDataSimpleRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 管理后台 - 字典数据
 * </p>
 *
 * @author lidy
 * @since 2024-05-19
 */
@Tag(name = "管理后台 - 字典数据")
@RestController
@RequestMapping("/system/dictData")
@Validated
public class DictDataController {

    @Resource
    private DictDataService dictDataService;

    @GetMapping("/exportDictData")
    @Operation(summary = "导出字典数据")
    @PreAuthorize("@ss.hasPermission('system:dict:export')")
    @ApiAccessLog(operateType = OperateTypeEnum.EXPORT)
    public void exportDictData(HttpServletResponse response, @Valid DictDataPageReqVO reqVO) throws IOException {
        reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 查询导出数据
        List<DictDataDO> list = dictDataService.getDictDataPage(reqVO).getList();
        // 输出
        ExcelUtils.write(response, "字典数据.xlsx", "字典数据",
                DictDataRespVO.class, BeanUtils.toBean(list, DictDataRespVO.class));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除字典数据")
    @Parameter(name = "dictId", description = "字典编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:dict:delete')")
    public CommonResult<Boolean> deleteDictData(@RequestParam("dictId") Long dictId) {
        dictDataService.deleteDictData(dictId);
        return CommonResult.success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "修改字典数据")
    @PreAuthorize("@ss.hasPermission('system:dict:update')")
    public CommonResult<Boolean> updateDictData(@Valid @RequestBody DictDataSaveReqVO updateReqVO) {
        dictDataService.updateDictData(updateReqVO);
        return CommonResult.success(true);
    }

    @PostMapping("/create")
    @Operation(summary = "创建字典数据")
    @PreAuthorize("@ss.hasPermission('system:dict:create')")
    public CommonResult<Long> createDictData(@Valid @RequestBody DictDataSaveReqVO createReqVO) {
        Long dictId = dictDataService.createDictData(createReqVO);
        return CommonResult.success(dictId);
    }

    @GetMapping("/get")
    @Operation(summary = "获得字典数据详细信息")
    @Parameter(name = "dictId", description = "字典编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:dict:query')")
    public CommonResult<DictDataRespVO> getDictData(@RequestParam("dictId") Long dictId) {
        DictDataDO dictDataDO = dictDataService.getDictData(dictId);
        return CommonResult.success(BeanUtils.toBean(dictDataDO, DictDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得字典数据分页列表")
    @PreAuthorize("@ss.hasPermission('system:dict:query')")
    public CommonResult<PageResult<DictDataRespVO>> getDictDataPage(@Valid DictDataPageReqVO reqVO) {
        PageResult<DictDataDO> pageResult = dictDataService.getDictDataPage(reqVO);
        return CommonResult.success(BeanUtils.toBean(pageResult, DictDataRespVO.class));
    }

    @GetMapping(value = {"/listAllSimple", "simpleList"})
    @Operation(summary = "获得全部字典数据列表", description = "一般用于管理后台缓存字典数据在本地")
    // 无需添加权限认证，因为前端全局都需要
    public CommonResult<List<DictDataSimpleRespVO>> getSimpleDictDataList() {
        List<DictDataDO> list = dictDataService.getDictDataList(CommonStatusEnum.ENABLE.getStatus(), null);
        return CommonResult.success(BeanUtils.toBean(list, DictDataSimpleRespVO.class));
    }

}
